<template>
  <div id="styleMarage">
    <div id="mainBox">
      <div id="boxTit">
        <span
          v-if="mid != null"
          class="tit"
        >修改模板</span>
        <span
          v-else
          class="tit"
        >添加模板</span>
      </div>
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
      >
        <el-form-item
          ref="name"
          label="模板名称"
        >
          <el-input v-model="form.name"/>
        </el-form-item>

        <el-form-item label="附加值">
          <div>
            <div id="tit">
              <div><span>名称</span></div>
              <div><span>路径</span></div>
              <div class="pro"><span>文件</span></div>
              <div class="addbtn">
                <i
                  class="el-icon-circle-plus-outline"
                  @click="appends"
                />
              </div>
            </div>
            <div
              v-for="(item,index) in arrays"
              ref="arrays"
              :key="index"
              class="subjoin"
            >

              <div class="sub">
                <el-input v-model="item.name"/>
              </div>
              <div class="sub">
                <el-input v-model="item.classPath"/>
              </div>
              <div class="sub pro">

                <div
                  v-if="item.filePath != '' "
                  class="btn"
                >
                  <el-popover
                    :content="item.filePath"
                    placement="top-start"
                    trigger="hover"
                  >
                    <i
                      slot="reference"
                      class="el-icon-circle-check"
                    />
                  </el-popover>
                </div>

                <div
                  v-else
                  class="btn"
                >
                  <i class="el-icon-circle-plus-outline"/>
                  <input
                    id="uploads"
                    ref="file"
                    type="file"
                    name=""
                    @change="upload(index)"
                  >
                </div>
                <div
                  class="closenBtn"
                  @click="closenBtn(index)"
                >
                  <i class="el-icon-circle-close"/>
                </div>
              </div>
              <div
                class="closenSubjoin"
                @click="closenSubjoin(index)"
              >
                <i class="el-icon-error"/>
              </div>
            </div>
          </div>

        </el-form-item>

        <el-form-item label="备注">
          <el-input
            v-model="form.templateDesc"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div class="btns">
        <el-button
          class="next"
          @click="onSubmit"
        >提交</el-button>
        <el-button
          class="back"
          @click="onBack"
        >取消</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { gtmSave, gtmDesc, gtmList } from '@/api/page'

export default {
  data() {
    return {
      form: {
        name: '',
        desc: ''
      },
      data: {
        current: 1,
        size: 5,
        condition: {
          templateType: 'STYLE'
        }
      },
      arrays: [],
      mid: ''
    }
  },
  mounted() {
    this.mid = localStorage.getItem('mid')
    if (this.mid != null) {
      var arr = {
        id: this.mid,
        templateType: 'STYLE'
      }

      gtmDesc(arr)
        .then(response => {
          if (response.data.code == 'SUCCESS') {
            this.form = response.data.data
            if (response.data.data.sources == undefined) {
              this.arrays = []
            } else {
              this.arrays = response.data.data.sources
            }

            if (this.form.templateDesc == 'undefined') {
              this.form.templateDesc = ''
            } else {
              this.form.templateDesc = response.data.data.templateDesc
            }
          } else {
            alert(response.data.message)
          }
        })
        .catch(error => {
          alert(error.response.data.message)
        })
    }
  },
  methods: {
    onSubmit() {
      this.req()
      const postData = new FormData()
      const config = {
        headers: { 'Content-Type': 'multipart/form-data' }
      }
      postData.append('name', this.form.name)
      postData.append('templateDesc', this.form.templateDesc)
      postData.append('templateType', 'STYLE')
      if (this.mid != null) {
        postData.append('id', this.mid)
      }

      for (var i = 0; i < this.arrays.length; i++) {
        postData.append('sources[' + i + '].name', this.arrays[i].name)
        if (this.arrays[i].pro == undefined) {
          postData.append(
            'sources[' + i + '].filePath',
            this.arrays[i].filePath
          )
        } else {
          postData.append('sources[' + i + '].filePath', '')
          if (this.arrays[i].filePath == '') {
            alert('请选择文件')
            return
          }
        }
        postData.append(
          'sources[' + i + '].classPath',
          this.arrays[i].classPath
        )
        postData.append(i, this.arrays[i].pro)
      }
      if (this.req() != true) {
        gtmSave(postData, config)
          .then(response => {
            if (response.data.code == 'SUCCESS') {
              if (this.mid != null) {
                localStorage.removeItem('mid')
              }
              this.openFullScreen()
              setTimeout(() => {
                this.loading.close()
                this.$router.push('/style/index')
              }, 1000)
            } else {
              alert(response.data.message)
            }
          })
          .catch(error => {
            alert(error.response.data.message)
          })
      }
    },
    openFullScreen() {
      this.loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    },
    req() {
      if (this.form.name == '') {
        this.$refs.name.$el.childNodes[0].style.color = 'red'
        this.$refs.name.$el.childNodes[1].childNodes[0].childNodes[1].style.borderColor =
          'red'
      } else {
        this.$refs.name.$el.childNodes[0].style.color = '#606266'
        this.$refs.name.$el.childNodes[1].childNodes[0].childNodes[1].style.borderColor =
          '#c0c4cc'
      }
      for (var j = 0; j < this.arrays.length; j++) {
        if (
          this.$refs.arrays[j].childNodes[0].childNodes[0].childNodes[1]
            .value == ''
        ) {
          this.$refs.arrays[
            j
          ].childNodes[0].childNodes[0].childNodes[1].style.borderColor = 'red'
        } else {
          this.$refs.arrays[
            j
          ].childNodes[0].childNodes[0].childNodes[1].style.borderColor =
            '#c0c4cc'
        }
        if (
          this.$refs.arrays[j].childNodes[2].childNodes[0].childNodes[1]
            .value == ''
        ) {
          this.$refs.arrays[
            j
          ].childNodes[2].childNodes[0].childNodes[1].style.borderColor = 'red'
        } else {
          this.$refs.arrays[
            j
          ].childNodes[2].childNodes[0].childNodes[1].style.borderColor =
            '#c0c4cc'
        }
        if (this.form.name != '') {
          if (
            this.$refs.arrays[j].childNodes[0].childNodes[0].childNodes[1]
              .value != ''
          ) {
            if (
              this.$refs.arrays[j].childNodes[2].childNodes[0].childNodes[1]
                .value != ''
            ) {
            } else {
              return true
            }
          } else {
            return true
          }
        } else {
          return true
        }
      }
    },
    appends() {
      var arrObj = { name: '', classPath: '', pro: {}, filePath: '' }
      this.arrays.push(arrObj)
    },
    upload(index) {
      var event = window.event || arguments[0]
      var uplist = event.target.files
      for (var i = 0; i < this.arrays.length; i++) {
        this.arrays[index].pro = uplist[0]
        this.arrays[index].filePath = uplist[0].name
      }
    },
    closenSubjoin(index) {
      this.arrays.splice(index, 1)
    },
    closenBtn(index) {
      this.arrays[index].filePath = ''
      this.arrays[index].pro = {}
    },
    onBack() {
      localStorage.removeItem('mid')
      this.$router.push('/style/index')
    }
  }
}
</script>
<style>
#styleMarage .el-input {
  width: 500px;
}
#styleMarage .el-textarea {
  width: 500px;
}
#styleMarage .subjoin {
  height: 40px;
  width: 500px;
}
#styleMarage .subjoin .el-input {
  width: 100px;
}
#styleMarage .el-input__inner {
  padding: 0 10px;
}
#styleMarage .sub {
  position: relative;
  float: left;
  width: 150px;
  height: 40px;
  border-bottom: 1px solid #ccc;
}
#styleMarage .sub .el-input__inner {
  height: 28px;
}

#styleMarage .sub .el-button--medium {
  padding: 3px 10px;
}
#styleMarage .sub .el-button {
  width: 100px;
  height: 28px;
}
#styleMarage #uploads {
  position: absolute;
  width: 30px;
  height: 36px;
  opacity: 0;
  left: 0;
  top: 0;
}
#styleMarage #tit {
  width: 530px;
  height: 30px;
  position: relative;
}
#styleMarage #tit div {
  width: 150px;
  height: 28px;
  line-height: 28px;
  float: left;
  border-bottom: 1px solid #ccc;
}
#styleMarage #tit div span {
  margin-left: 5px;
}

#styleMarage .pro {
  width: 180px;
}
#styleMarage #tit .pro {
  width: 180px;
}
#styleMarage .closenSubjoin {
  color: crimson;
  position: absolute;
  left: -30px;
  font-size: 20px;
}
#styleMarage .btn {
  position: relative;
  text-align: center;
  width: 30px;
  color: aqua;
  font-size: 20px;
  float: left;
}
#styleMarage .closenBtn {
  color: cornflowerblue;
  text-align: center;
  width: 30px;
  font-size: 20px;
  float: left;
}
#styleMarage .el-form {
  margin: 90px 150px;
  margin-bottom: 20px;
}
#styleMarage #mainBox {
  margin-left: 15px;
  margin-top: 15px;
  width: 100%;
  border: 1px solid #623f18;
}
#styleMarage #mainBox #boxTit {
  width: 100%;
  height: 40px;
  border-bottom: 1px solid #623f18;
}
#styleMarage #mainBox #boxTit .tit {
  margin-left: 20px;
  font-size: 20px;
  color: #623f18;
  line-height: 40px;
}
#styleMarage #mainBox .btns {
  text-align: center;
  border-top: 1px solid #e5e5e5;
  padding-top: 10px;
  padding-bottom: 10px;
  background: #f5f5f5;
}
#styleMarage #mainBox .btns .el-button {
  margin-right: 30px;
}
#styleMarage .next {
  background: #623f18;
  color: #fff;
}
#styleMarage .back {
  border-color: #623f18;
  color: #623f18;
}

#styleMarage .addbtn {
  width: auto;
  position: absolute;
  right: 0;
  top: 0;
  font-size: 20px;
}
#styleMarage .addbtn i {
  cursor: pointer;
}
</style>
